﻿<UserControl x:Class="SPIWareMVVM.Views.ProductionList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:controls="clr-namespace:SPIWareMVVM.Views"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:enums="clr-namespace:DataAccess;assembly=DataAccess"
             xmlns:me="clr-namespace:SPIWareMVVM.ViewModels" 
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             xmlns:views="clr-namespace:SPIWareMVVM.Views"
             xmlns:converters="clr-namespace:SPIWareMVVM.Views.Conversions"
             xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ObjectDataProvider x:Key="productionVM" ObjectType="{x:Type me:ProductionViewModel}"/>
        <CollectionViewSource x:Key="workShifts">
            <CollectionViewSource.Source>
                <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type enums:WorkShifts}">
                    <ObjectDataProvider.MethodParameters>
                        <x:Type TypeName="enums:WorkShifts"/>
                    </ObjectDataProvider.MethodParameters>
                </ObjectDataProvider>
            </CollectionViewSource.Source>
        </CollectionViewSource>
        <ObjectDataProvider
            x:Key="getTotalCount"
            ObjectType="{x:Type me:ProductionViewModel}"
            MethodName="GetTotalCount">
            <!--<ObjectDataProvider.MethodParameters>
                <system:Int32>0</system:Int32>
                <system:Int32>0</system:Int32>
                <system:Int32>0</system:Int32>
                <system:Int32>0</system:Int32>
                <system:Int32>0</system:Int32>
            </ObjectDataProvider.MethodParameters>-->
        </ObjectDataProvider>
        <converters:SumConverter x:Key="sumConverter" />
        <converters:RunTimeConverter x:Key="runTimeConverter"/>
        <converters:ShotsMadeConverter x:Key="shotsMadeConverter"/>
        <converters:PartsCountConverter x:Key="partsCountConverter"/>
        <converters:ScrapCountConverter x:Key="scrapCountConverter"/>
        <converters:GoodCountConverter x:Key="goodCountConverter"/>
        <converters:BalanceConverter x:Key="balanceConverter"/>
        
        <converters:MultiValueConverter x:Key="multiValueConverter"/>
        <ObjectDataProvider x:Key="getTotalRunTime" ObjectType="{x:Type me:ProductionViewModel}" MethodName="GetTotalRunTime">
            <ObjectDataProvider.MethodParameters>
                <enums:MachineProduction/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="getMachineRunTimes" ObjectType="{x:Type me:ProductionViewModel}" MethodName="GetMachineRunTimes">
            <ObjectDataProvider.MethodParameters>
                <enums:MachineProduction/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="getOperatorRunTimes" ObjectType="{x:Type me:ProductionViewModel}" MethodName="GetOperatorRunTimes">
            <ObjectDataProvider.MethodParameters>
                <enums:MachineProduction/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

    </UserControl.Resources>
    <DockPanel>
        <ToolBar DockPanel.Dock="Top">
            <TextBlock DockPanel.Dock="Top" Text="Daily Production" Background="Azure" Width="Auto"/>
            <Separator/>
        </ToolBar>

        <StackPanel DockPanel.Dock="Left" Orientation="Vertical">
            <TextBlock Text="Floor Run Time: "/>
            <TextBlock Text="{Binding Source={StaticResource getTotalRunTime}}" x:Name="tbTotalRunTime"/>
            <TextBlock Height="24" Background="Navy" Text="Machine Usage" Foreground="White"/>
            <ListBox ItemsSource="{Binding Source={StaticResource getMachineRunTimes}}" x:Name="tbMachineRunTimes" IsEnabled="False" />
            <TextBlock Height="24" Background="Navy" Text="Operator Usage" Foreground="White"/>
            <ListBox ItemsSource="{Binding Source={StaticResource getOperatorRunTimes}}" x:Name="tbOperatorRunTimes" IsEnabled="False"/>
        </StackPanel>
        <ListBox DockPanel.Dock="Left" x:Name="lbProduction" ItemsSource="{Binding Path=AllProductions, Source={StaticResource productionVM}, Mode=TwoWay}"
                 SelectionChanged="Production_SelectionChanged">
            <ListBox.SelectedValue>
                <MultiBinding Converter="{StaticResource multiValueConverter}">
                    <Binding Source="{StaticResource getTotalRunTime}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"/>
                    <Binding Source="{StaticResource getMachineRunTimes}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"/>
                    <Binding Source="{StaticResource getOperatorRunTimes}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"/>
                </MultiBinding>
            </ListBox.SelectedValue>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding ApplicationUser.UserName}"/>
                        <TextBlock Text=" "/>
                        <TextBlock Text="{Binding RunTime}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        
        <DockPanel x:Name="dpProductionEdit" DockPanel.Dock="Top" Visibility="Collapsed" DataContext="{Binding SelectedItem, ElementName=lbProduction}">
            <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                <TextBlock Text="("/>
                <TextBlock Text="{Binding Path=LogId, Mode=TwoWay}"/>
                <TextBlock Text=") "/>
                <TextBlock Text="{Binding Path=MachinePress.MachineNumber, Mode=TwoWay}"/>
                <TextBlock Text=" "/>
                <TextBlock Text="{Binding Path=Part.Description, Mode=TwoWay}"/>
            </StackPanel>
            <ToolBar DockPanel.Dock="Left" Background="MidnightBlue"/>
            <Grid DockPanel.Dock="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="85"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Column="0" Grid.Row="0" Text="Shift: "/>
                <TextBlock Grid.Column="0" Grid.Row="1" Text="Press Machine: "/>
                <TextBlock Grid.Column="0" Grid.Row="2" Text="Operator: "/>
                <TextBlock Grid.Column="0" Grid.Row="3" Text="Work Order: "/>

                <StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal">
                    <ComboBox HorizontalAlignment="Right" SelectionChanged="Shift_SelectionChanged"
                              ItemsSource="{Binding Source={StaticResource workShifts}}" 
                              SelectedItem="{Binding Path=SelectedItem.Shift, ElementName=lbProducton}"/>
                    <TextBlock Text="{Binding Shift, Mode=TwoWay}"/>
                </StackPanel>
                <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal">
                    <ComboBox x:Name="cboPressMachines" 
                              ItemsSource="{Binding Path=ocMachines, Source={StaticResource productionVM}}"
                              SelectedItem="{Binding Path=SelectedItem.MachinePress, Source={StaticResource productionVM}}"
                              SelectionChanged="PressMachine_Changed"/>
                    <TextBlock Text="{Binding MachinePress.MachineNumber, Mode=TwoWay}"/>
                </StackPanel>
                <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
                    <ComboBox x:Name="cboOperators" SelectionChanged="Operator_SelectionChanged"
                              ItemsSource="{Binding Path=ocOperators, Source={StaticResource productionVM}}"
                              SelectedItem="{Binding Path=SelectedItem.ApplicationUser, Source={StaticResource productionVM}}"/>
                    <TextBlock Text="{Binding ApplicationUser.UserName, Mode=TwoWay}"/>
                </StackPanel>
                <StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal">
                    <ComboBox x:Name="cboWorkOrders" SelectionChanged="WorkOrder_SelectionChanged"
                              ItemsSource="{Binding Path=ocWorkOrders,Source={StaticResource productionVM}}"
                              SelectedItem="{Binding Path=SelectedItem.WorkOrder, Source={StaticResource productionVM}}"/>
                    <Button x:Name="btnViewClosedWO" Click="btnViewClosedWO_Clicked">
                        <Image Source="..\Images\ViewAll.png" Height="24" Width="24"/>
                    </Button>
                    <TextBlock Text="{Binding WorkOrder.WorkOrderId, Mode=TwoWay}"/>
                </StackPanel>
            </Grid>
            <StackPanel x:Name="spLogHeader" DockPanel.Dock="Top" Orientation="Horizontal">
                <TextBlock Text="{Binding Part.Description, Mode=TwoWay}" Background="MintCream"/>
                <TextBlock Text=" " Background="MintCream"/>
                <TextBlock Text="{Binding Part.PartsPerBox, Mode=TwoWay}" x:Name="txtPartsPerBox" Background="Cornsilk"/>
                <TextBlock Text=" Parts / Box " Background="Cornsilk"/>
                <TextBlock Text="{Binding Part.PartsPerShot, Mode=TwoWay}" x:Name="partsPerShot" Background="LightCyan"/>
                <TextBlock Text=" Parts / Shot " Background="LightCyan"/>
            </StackPanel>

            <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                <GroupBox Header="Beginning of Shift">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="85"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="Start Date/Time: "/>
                        <TextBlock Grid.Column="0" Grid.Row="1" Text="Start Count: "/>
                        <TextBlock Grid.Column="0" Grid.Row="2" Text="Parts in Box: "/>

                        <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding StartTime, Mode=TwoWay}"/>
                        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding StartCount, Mode=TwoWay}"/>
                        <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding StartPartsInBox, Mode=TwoWay}"/>
                    </Grid>
                </GroupBox>
                <GroupBox Header="End of Shift">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="85"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="Parts in Box: "/>
                        <TextBlock Grid.Column="0" Grid.Row="1" Text="Boxes Completed: "/>
                        <TextBlock Grid.Column="0" Grid.Row="2" Text="End Date/Time: "/>
                        <TextBlock Grid.Column="0" Grid.Row="3" Text="End Count: "/>

                        <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding EndPartsInBox, Mode=TwoWay}"/>
                        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding BoxesCompleted, Mode=TwoWay}"/>
                        <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding EndTime, Mode=TwoWay}"/>
                        <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding EndCount, Mode=TwoWay}"/>
                    </Grid>
                </GroupBox>
                <GroupBox Header="Totals">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="85"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="RunTime: "/>
                        <TextBlock Grid.Column="0" Grid.Row="1" Text="Shots Made: "/>
                        <TextBlock Grid.Column="0" Grid.Row="2" Text="Good Parts: "/>
                        <TextBlock Grid.Column="0" Grid.Row="3" Text="Scrap: "/>
                        <TextBlock Grid.Column="0" Grid.Row="4" Text="Balance: "/>

                        <!--<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding RunTime}"/>
                        <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding ShotsMade, Mode=TwoWay}"/>
                        <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding GoodParts, Mode=TwoWay}"/>
                        <TextBlock Grid.Column="1" Grid.Row="3" Text="{Binding Scrap, Mode=TwoWay}"/>-->
                        <TextBlock Grid.Column="1" Grid.Row="0">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource runTimeConverter}">
                                    <Binding Path="StartTime"/>
                                    <Binding Path="EndTime"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Grid.Column="1" Grid.Row="1" x:Name="shotsMade">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource shotsMadeConverter}">
                                    <Binding Path="StartCount"/>
                                    <Binding Path="EndCount"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Visibility="Collapsed" x:Name="partsCount">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource partsCountConverter}">
                                    <Binding Path="EndCount"/>
                                    <Binding Path="StartCount"/>
                                    <Binding Path="Text" ElementName="partsPerShot"/>
                                    <!--<Binding Path="StartPartsInBox"/>
                                    <Binding Path="EndPartsInBox"/>-->
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Grid.Column="1" Grid.Row="2">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource goodCountConverter}">
                                    <Binding Path="EndCount"/>
                                    <Binding Path="StartCount"/>
                                    <Binding Path="Text" ElementName="partsPerShot"/>
                                    <Binding Path="Text" ElementName="scrapCount"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Grid.Column="1" Grid.Row="3" x:Name="scrapCount">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource scrapCountConverter}">
                                    <Binding Path="Startup"/>
                                    <Binding Path="BlackSpecks"/>
                                    <Binding Path="Splay"/>
                                    <Binding Path="Short"/>
                                    <Binding Path="Discoloration"/>
                                    <Binding Path="Scratching"/>
                                    <Binding Path="Cracks"/>
                                    <Binding Path="Other"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Grid.Column="1" Grid.Row="4" x:Name="balance">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource balanceConverter}">
                                    <Binding Path="BoxesCompleted"/>
                                    <Binding Path="Text" ElementName="txtPartsPerBox"/>
                                    <Binding Path="StartPartsInBox"/>
                                    <Binding Path="EndPartsInBox"/>
                                    <Binding Path="Text" ElementName="shotsMade"/>
                                    <Binding Path="Text" ElementName="scrapCount"/>
                                    <Binding Path="Part.PartsPerShot"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </Grid>
                </GroupBox>
            </StackPanel>
            <GroupBox DockPanel.Dock="Top" Header="Scrap Count">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Grid.Row="0" Text="StartUp"/>
                    <TextBlock Grid.Column="1" Grid.Row="0" Text="Color Specs"/>
                    <TextBlock Grid.Column="2" Grid.Row="0" Text="Splay"/>
                    <TextBlock Grid.Column="3" Grid.Row="0" Text="Short Shot"/>
                    <TextBlock Grid.Column="4" Grid.Row="0" Text="Discoloration"/>
                    <TextBlock Grid.Column="5" Grid.Row="0" Text="Scratch / Cut"/>
                    <TextBlock Grid.Column="6" Grid.Row="0" Text="Flow Line / Cold Slug"/>
                    <TextBlock Grid.Column="7" Grid.Row="0" Text="Other"/>

                    <TextBox Grid.Column="0" Grid.Row="1" Text="{Binding Startup}"/>
                    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding BlackSpecks}"/>
                    <TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Splay}"/>
                    <TextBox Grid.Column="3" Grid.Row="1" Text="{Binding Short}"/>
                    <TextBox Grid.Column="4" Grid.Row="1" Text="{Binding Discoloration}"/>
                    <TextBox Grid.Column="5" Grid.Row="1" Text="{Binding Scratching}"/>
                    <TextBox Grid.Column="6" Grid.Row="1" Text="{Binding Cracks}"/>
                    <TextBox Grid.Column="7" Grid.Row="1" Text="{Binding Other}"/>
                </Grid>
            </GroupBox>
        </DockPanel>
    </DockPanel>
</UserControl>
